package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.test01.service.User01Service;
import com.test02.service.User02Service;

@RestController
public class IndexController {
 
	@Autowired
	private User01Service user01Service;
	@Autowired
	private User02Service user02Service;
	
	/****
	 * user01Service对应的数据库，可以保存 
	 * user02Service对应的数据库，不能保存
	 * DataSourceConfig2的类使用@Primary注解，说明这个使用了事务
	 * @return
	 */
	@Transactional
	@RequestMapping("/add")
	public String add() {
		System.out.println("add。..................");
		user01Service.addUser("test001", 19);
		int i=1/0;
		user02Service.addUser("test002", 19);
		return "success";
	}
	
	/****
	 * user02Service保存了数据，但是user02Service有事务 直接回滚
	 * @return
	 */
	@Transactional
	@RequestMapping("/add2")
	public String add2() {
		System.out.println("add2。..................");
		user02Service.addUser("test002", 19);
		int i=1/0;
		user01Service.addUser("test001", 19);
		return "success";
	}
	
	@Transactional
	@RequestMapping("/add3")
	public String add3() {
		System.out.println("add。..................");
		user01Service.addUser("test001", 19);
		user02Service.addUser("test002", 19);
		return "success";
	}

}
